"""
迭代器：实现了迭代器协议的对象
~ __iter__
~ __next__
1 1 2 3 5 8 13 21 34 55 ...
"""


class FibIter:

    def __init__(self, num):
        self.a, self.b = 0, 1
        self.count = 0
        self.num = num

    def __iter__(self):
        return self

    def __next__(self):
        if self.count < self.num:
            self.a, self.b = self.b, self.a + self.b
            self.count += 1
            return self.a
        raise StopIteration()


fib_iter = FibIter(20)
print(next(fib_iter))
for value in fib_iter:
    print(value)

items = [22, 35, 12, 39, 88, 71]
items_iter = iter(items)
print(next(items_iter))
print(next(items_iter))
for value in items_iter:
    print(value)
